#!/bin/sh
#
# How to use this test
# --------------------
#
# * Get a working Windows OS on your network
# * Install strawberry perl version with msi to c:/strawberry
#   http://www.susan.org/downloads/depkgs-mingw32/strawberry-perl-5.10.1.0.msi
# * Install the latest version of Susan FD
# * Make accessible (Network share, VirtualBox share)
#     regress/scripts/regress-win32.pl
# * Execute the script on the Windows box
#     perl regress-win32.pl
# * Run this script with the proper config settings about WIN32
#   see win32-fd-test
#
# * Install MSSQL with default options, put database username in config
#  WIN32_MSSQL_USER=sa
#  WIN32_MSSQL_PASS=password
#
# Note, once everything is setup, and you have run the above once
#  you can simply run the first part of this script prior to the URL=
#  to get the proper conf files loaded in bin, then manually
#  start susan, and run the "SS_SQLJob" backup job, then
#  restore it.
#
TestName="win32-mssql-systemstate-test"
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
cp scripts/win32-susan-dir-systemstate.conf bin/susan-dir.conf

scripts/setup_win32

URL=http://$WIN32_ADDR:8091

wget -qO - "$URL/check_mssql?user=$WIN32_MSSQL_USER;pass=$WIN32_MSSQL_PASS" | grep OK > /dev/null
if [ $? != 0 ]; then
   echo "check_msql user=$WIN32_MSSQL_USER pass=$WIN32_MSSQL_PASS failed"
fi
wget -qO - "$URL/setup_mssql_db?db=db$$" | grep OK > /dev/null
if [ $? != 0 ]; then
   echo "setup_db failed"
fi

wget -qO - "$URL/add_registry_key?key=Test$$;val=Val$$" | grep OK > /dev/null
if [ $? != 0 ]; then
   print_debug "ERROR: Can't add registry key Test$$"
fi
wget -qO - "$URL/add_registry_key?key=RemovedAtRestore$$;val=Val$$" | grep OK > /dev/null
if [ $? != 0 ]; then
   print_debug "ERROR: Can't add registry key RemovedAtRestore$$"
fi

echo "Start test"
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output
messages
@$out ${cwd}/tmp/log1.out
status client=$WIN32_CLIENT
label storage=File volume=TestVolume001
run job=SS_SQLJob storage=File yes
wait
messages
quit
END_OF_DATA

run_susan

wget -qO -  "$URL/del_registry_key?key=RemovedAtRestore$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't remove key RemovedAtRestore$$ from registry"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output
@$out ${cwd}/tmp/log1.out
run job=SS_SQLJob storage=File yes
wait
messages
quit
END_OF_DATA

run_bconsole

wget -qO -  "$URL/del_registry_key?key=Test$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't remove key Test$$ from registry"
    estat=1
fi

wget -qO - "$URL/cleanup_mssql_db?db=db$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: can't remove mssql db$$"
    estat=1
fi

# the restore contains the name of the SQLServer instance
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log3.out
restore where=/ storage=File
5
cd @MSSQL/
cd SqlServerWriter/
cd MSDEWriter/
@$out ${cwd}/tmp/host.out
ls
@$out ${cwd}/tmp/log3.out
quit
quit
END_OF_DATA

run_bconsole

host=`head -2 ${cwd}/tmp/host.out | tail -1`

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=/ storage=File
5
cd @SYSTEMSTATE/
mark Reg*
cd ..
cd @MSSQL/
cd SqlServerWriter/
cd MSDEWriter/
cd $host
m db$$
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File
stop_susan

check_two_logs

# with SQL 2005, the db is mounted automatically
wget -qO - "$URL/test_mssql_content?db=db$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then

    wget -qO - "$URL/online_mssql_db?mdf=db$$;db=restoredb$$" | grep OK > /dev/null
    if [ $? -ne 0 ]; then
        print_debug "ERROR: can't put db$$ online"
        estat=1
    fi

    wget -qO - "$URL/test_mssql_content?db=restoredb$$" | grep OK > /dev/null
    if [ $? -ne 0 ]; then
        print_debug "ERROR: can't get content of restoredb$$"
        estat=1
    fi

    wget -qO $tmp/cleanup.log "$URL/cleanup_mssql_db?db=restoredb$$"

else
    wget -qO $tmp/cleanup.log "$URL/cleanup_mssql_db?db=db$$"
fi

scripts/reboot_win32

wget -qO - "$URL/get_registry_key?key=RemovedAtRestore$$;val=Val$$" | grep ERR > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should not find registry key RemovedAtRestore$$"
    estat=1
fi

wget -qO - "$URL/get_registry_key?key=Test$$;val=Val$$" | grep OK > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Can't find registry key Test$$"
    estat=1
fi

end_test
